<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>平台银行管理</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" media="all">
    <style>
        .bankInfo {
            padding:0 20px;
        }

        td[data-field="imgUrl"] div {
            height: 120px;
        }

        .lay-form {
            padding: 20px;
        }

        .bankInfo {
            background-color: #fff;
        }
        .searchInput {
            padding-top: 20px;
            display: flex;
            align-items: flex-end;
        }

        .searchInput input {
            width: 200px;
        }

        .searchInput .layui-form-label {
            padding: 9px 0;
        }
    </style>
</head>

<body>
<!-- 搜索 -->
<div></div>
<div class="searchInput">
    <form class="layui-form" action="" lay-filter="resets">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">开户行：</label>
                <input class="layui-input" name="bankName" placeholder="请输入开户行" autocomplete="off">
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">账号：</label>
                <input class="layui-input" name="bankNum" placeholder="请输入账号" autocomplete="off">
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">持卡人：</label>
                <input class="layui-input" name="bankUserName" placeholder="请输入持卡人" autocomplete="off">
            </div>
            <button class="layui-btn" data-type="reload" lay-submit="" lay-filter="searchForm">搜索</button>
            <button class="layui-btn layui-btn-primary" data-type="reload" lay-submit="" lay-filter="reset">重置</button>
        </div>
    </form>
</div>
<div class="bankInfo">
    <table lay-filter="bankTable" class="layui-hide" id="bankTable"></table>
</div>

</body>

<!-- 添加 -->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>

<!-- 操作 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.all.min.js"></script>
<script src="../utils.js"></script>
<script>

    layui.use(['table', 'upload', 'form', 'jquery'], function() {
        var table = layui.table,
            upload = layui.upload,
            form = layui.form,
            $ = layui.jquery;

        table.render({
            elem: '#bankTable',
            toolbar: "#toolbarDemo",
            url: window.baseUrl + '/platformBankCard/getBankList',
            method: 'post',
            headers: {
                Authorization: localStorage.token
            },
            contentType: 'application/json',
            response: {
                statusCode: 200 //规定成功的状态码，默认：0
            },
            cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,
            cols: [
                [{
                    field: 'bankId',
                    width: 80,
                    title: 'ID',
                    sort: true
                }, {
                    field: 'bankName',
                    title: '开户行'
                }, {
                    field: 'bankNum',
                    title: '账号'
                }, {
                    field: 'bankUserName',
                    title: '持卡人'
                }, {
                    title: '设置收款',
                    width: 100,
                    templet: function(res) {
                        return '<input type="checkbox" ' + (res.stop == 0 ? 'checked=""' : "") + '" lay-filter="switchTest" lay-skin="switch" lay-text="开启|关闭" value="' + res.stop + '" data-id="' + res.id + '"/>'
                    }
                }, {
                    field: 'createTime',
                    title: '添加时间'
                }, {
                    fixed: 'right',
                    title: '操作',
                    toolbar: '#barDemo',
                    width: 150
                }]
            ]
        });

        // 搜索
        form.on("submit(searchForm)", function(e) {
            table.reload('bankTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                },
                where: e.field
            });

            return false;
        })
        form.on("submit(reset)", function(e) {
            table.reload('bankTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                },
                where: null
            });

            form.val("resets", {
                bankId:'',
                bankNum:'',
                bankUserName:'',
            });

            return false;
        })

        //工具栏事件
        table.on('toolbar(bankTable)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event) {
                case 'add':
                    $("#bankId").val("");
                    $("#imgUrl").val("");
                    //页面层
                    showLayer();
                    break;
            };
        });

        // 编辑和删除
        table.on("tool(bankTable)", function(data) {
            switch(data.event) {
                case "del":
                    layer.confirm('确定删除？', function(index) {
                        ajax({
                            path: "/platformBankCard/delete?id=" + data.data.id,
                            type: 'GET',
                            success: function(res) {
                                layer.closeAll();
                                layer.msg("删除成功");
                                table.reload("bankTable");
                            }
                        }, $)
                    });
                    break;
                case "edit":
                    showLayer(data.data);
                    $('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', data.data.bankImg);
                    break;
            }
        })

        function showLayer(data) {
            //获取银行卡信息
            ajax({
                path: "/indexUser/getBankInfo",
                type: 'GET',
                success: function(res) {
                    var value = res.data;
                    res.forEach(el => {
                        $('#bankList').append(`<option value="${el.bankId}" data-name ="${el.bankName}">${el.bankName}</option>`)
                })
                    form.render();
                }
            }, $)
            layer.open({
                type: 1,
                title: "添加银行信息",
                area: ['650px', '600px'], //宽高
                content: `
                            <form class="layui-form lay-form" lay-filter="bankForm">
                                <div class="layui-form-item">
                                    <label class="layui-form-label" layui-required">开户行</label>
                                    <div class="layui-input-block">
                                            <select name="bankId"  lay-filter="bankId" id='bankList' lay-verify="required">
                                            </select>
                                        </div>
                                </div>
                                <div class="layui-form-item">
                                <label class="layui-form-label" layui-required">账号</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="bankNum" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" />
                                    </div>
                                </div>
                                 <input type="hidden" name="id" />

                                <div class="layui-form-item">
                                <label class="layui-form-label" layui-required">持卡人</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="bankUserName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                <label class="layui-form-label">作为收款账户</label>
                                    <div class="layui-input-block">
                                        <input type="checkbox" name="stop" lay-filter="switchStop" lay-skin="switch" lay-text="ON|OFF">
                                    </div>
                                </div>


                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="addBankSubmit">立即提交</button>
                                    <button type="reset" class="layui-btn layui-btn-prizmary">重置</button>
                                    </div>
                                </div>
                            </form>
                        `,
                success: function() {
                    if(data) {
                        form.val('bankForm', {
                            id: data.id,
                            bankId: data.bankId,
                            bankNum: data.bankNum,
                            bankUserName: data.bankUserName,
                            stop: data.stop == 0 ? true : false
                        });
                        form.render()
                    }
                }
            });
            // 开关
            form.on("switch(switchTest)", function(e){
                console.log(e)
                ajax({
                    path: "/platformBankCard/editStop",
                    data: {
                        id: $(e.elem).attr("data-id"),
                        stop: e.value == "1" ? 0 : 1
                    },
                    success: function (res) {
                        layer.msg("更新成功");
                        table.reload("bankTable");
                    }
                }, $)
            })
            uploads(upload, '#uploadimg');
            // form 提交
            let bankInfo;
            let switchFlag = 1;
            form.on('select(bankId)', function(data) {
                bankInfo = {
                    bankId: Number(data.value),
                    bankName: data.elem[data.elem.selectedIndex].text
                }
            })

            form.on('switch(switchStop)', function(data) {
                switchFlag = data.elem.checked ? 0 : 1
            })

            form.on("submit(addBankSubmit)", function(el) {
                delete el.field.file;
                console.log(el.field.stop)
                if(!el.field.stop ){
                    el.field.stop = 1;
                }else if(el.field.stop == 'on'){
                    el.field.stop = 0;
                }else{
                    el.field.stop = 1;
                }
                if(data) {
                    ajax({
                        path: "/platformBankCard/saveOrUpdate",
                        data: el.field,
                        success: function(res) {
                            layer.closeAll();
                            layer.msg("编辑成功");
                            table.reload("bankTable");
                        }
                    }, $)
                } else {
                    el.field.stop = switchFlag
                    Object.assign(el.field, bankInfo)
                    ajax({
                        path: "/platformBankCard/saveOrUpdate",
                        data: el.field,
                        success: function(res) {
                            layer.closeAll();
                            layer.msg("添加成功");
                            table.reload("bankTable");
                        }
                    }, $)
                }
                return false;
            })

        }
    });
</script>

</html>